<template>
	<view class="container">
		<!-- 小程序头部兼容 -->
		<!-- #ifdef MP -->

		<uni-nav-bar right-icon="qiandao" :fixed="true" @clickRight="mpNavbarClick">
			<view class="mp-search-box">
				<input class="ser-input" type="text" value="输入关键字搜索" />
			</view>
		</uni-nav-bar>
		<!-- #endif -->


		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 标题栏和状态栏占位符 -->
			<view class="titleNview-placing"></view>
			<!-- 背景色区域 -->
			<view class="titleNview-background" :style="{backgroundColor:titleNViewBackground}"></view>
			<swiper class="carousel" circular @change="swiperChange">
				<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item" @click="navToDetailPage(item)">
					<image :src="imgsrc + item.pic" />
				</swiper-item>
			</swiper>
			<!-- 自定义swiper指示器 -->
			<view class="swiper-dots">
				<text class="num num-font">{{swiperCurrent+1}}</text>
				<text class="sign">/</text>
				<text class="num num-font">{{swiperLength}}</text>
			</view>
		</view>
		<!-- 分类 -->
		<view class="cate-section">
			<!-- <view class="cate-item" @tap="toNoOpening">
				<image src="/static/temp/icon_game.png"></image>
				<text>牧耘农场</text>
			</view> -->
			<view class="cate-item" @tap="toNewGoods()">
				<image src="/static/temp/icon_new.png"></image>
				<text>本周新品</text>
			</view>
			<view class="cate-item" @tap="toPreSaleGoods()">
				<image src="/static/temp/icon_sale.png"></image>
				<text>预售专场</text>
			</view>
			<view class="cate-item" @tap="toMemberCentre()">
				<image src="/static/temp/icon_vip.png"></image>
				<text>会员中心</text>
			</view>
		</view>
		<!-- <view class="ad-1">
			<image src="/static/temp/ad1.jpg" mode="scaleToFill"></image>
		</view> -->
		<!-- 猜你喜欢 -->
		<view class="Hobby">
			<view class="s-header">
				<image class="s-img" src="/static/temp/icon_like.png" mode="widthFix"></image>今日福利
				<text>（每天可兑换一份）</text>
			</view>
			<view class="product-list">
				<view class="product" v-for="(item, index) in fortuneProductList" @tap="toPromotion(item)" :key="index">
					<image :src="imgsrc +item.home_pic" mode="widthFix"></image>
					<view class="name">
						<text>{{item.name}}</text>
					</view>
					<view class="info">
						<view class="price">
							<!-- <text class="currency"></text> -->
							<text>免费</text>
							<text class="slogan num-font">￥{{item.price}}</text>
						</view>
						<view style="font-size: 24upx;">
							<text>仅剩</text>
							<text class="red">{{item.exchange_stock}}</text>
							<text>份</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 秒杀楼层 -->
		<view class="seckill-section m-t">
			<view class="s-header">
				<image class="s-img" src="/static/temp/icon_time.png" mode="widthFix"></image>
				<text class="tip">限时秒杀</text>
				<text class="hour timer num-font">{{timeHour}}</text>
				<text class="colon">:</text>
				<text class="minute timer num-font">{{timeMinute}}</text>
				<text class="colon">:</text>
				<text class="second timer num-font">{{timeSecond}}</text>
				<!-- 暂时屏蔽更多 -->
				<!-- <span>更多<text class="yticon icon-you"></text></span> -->
			</view>
			<scroll-view class="floor-list" scroll-x>
				<view class="scoll-wrapper">
					<view v-for="(item, index) in homeFlashPromotion" :key="index" class="floor-item" @click="navToDetailPage(item)">
						<!-- mode="aspectFill" -->
						<image :src="imgsrc + item.home_pic"></image>
						<view class="info">
							<view class="title">
								<view class="name">{{item.name}}</view>
								<view class="snapUpNow">立即抢购</view>
							</view>
							<view class="price-number">
								<view class="price">
									<text class="currency">￥</text>
									<text class="num-font">{{item.price}}</text>
									<text class="slogan num-font">￥{{item.original_price}}</text>
								</view>
								<view class="sold">
									<text>已售 </text>
									<text class="num-font">{{item.sale}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>

		<!--热卖推荐-->
		<view class="bestSelling">
			<view class="s-header">
				<image class="s-img" src="/static/temp/icon_praise.png" mode="widthFix"></image><text>热卖推荐</text>
				<!-- 暂时屏蔽更多 -->
				<!-- <span>更多<text class="yticon icon-you"></text></span> -->
			</view>
			<view class="product-list">
				<view class="goods-info" v-for="(item, index) in hotProductList" @tap="navToDetailPage(item)" :key="index">
					<view class="img">
						<!-- mode="widthFix" -->
						<image :src="imgsrc + item.home_pic"></image>
					</view>
					<view class="info">
						<view class="title">
							<view class="name">{{item.name}}</view>
							<view class="snapUpNow">立即抢购</view>
						</view>
						<view class="price-number">
							<view class="price">
								<text class="currency">￥</text>
								<text class="num-font">{{item.price}} </text>
								<text class="slogan num-font">￥{{item.original_price}}</text>
							</view>
							<view class="hot">
								<!-- <image mode="widthFix" src="/static/img/home/hot.png"> -->
								<text>热度</text>
								<text class="num-font">95%</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import Api from '@/common/api';
	import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'

	let webView = null;

	export default {
		components: {
			uniNavBar
		},
		data() {
			return {
				imgsrc: Api.imgsrc,
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [],
				hotProductList: [],
				brandList: [], // 推荐品牌
				homeFlashPromotion: [], // 当前秒杀场次
				subjectList: [], //推荐专题
				newProductList: [],
				fortuneProductList: [],
				strtime: 0,
				timeDay: 0,
				timeHour: 0,
				timeMinute: 0,
				timeSecond: 0,
				showfortuneTask: false, //是否展示签到红点
				cartListLength: 0
			};
		},
		onLoad(options) {
			// #ifdef APP-PLUS
			const pages = getCurrentPages();
			const page = pages[pages.length - 1];
			webView = page.$getAppWebview();


			// #endif
			this.loadData();
		},
		onShow() {
			this.changeFortuneTaskStatus();
			this.getCartListLength();
		},
		watch: {
			cartListLength(newVal) {
				if (newVal > 0) {
					uni.setTabBarBadge({
						index: 2,
						text: `${newVal}`
					})
				} else {
					uni.removeTabBarBadge({
						index: 2
					})
				}
			}
		},
		computed: {
			...mapState(['hasLogin'])
		},
		methods: {
			/**
			 * 请求静态数据只是为了代码不那么乱
			 * 分次请求未作整合
			 */
			async loadData() {
				this.logining = true;
				let params = {};
				let data = await Api.apiCall('post', Api.index.homeIndex, params);
				this.logining = false;
				if (data) {
					this.carouselList = data.advertiseList || [];
					this.swiperLength = this.carouselList.length;
					this.titleNViewBackground = 'rgb(203, 87, 60)';
					// this.titleNViewBackground = carouselList[0].background;
					let newProductList = data.newProductList;
					this.newProductList = newProductList || [];
					let hotProductList = data.hotProductList;
					this.hotProductList = hotProductList || [];
					let homeFlashPromotion = data.homeFlashPromotion;
					this.homeFlashPromotion = homeFlashPromotion || [];
					let fortuneProductList = data.fortuneProductList;
					this.fortuneProductList = fortuneProductList || [];
					this.fortuneProductList.forEach(item => {
						//limit_stock：限兑换库存总数，ex_quantity 已兑换数量，exchange_stock 可兑换库存
						item.exchange_stock = item.limit_stock - item.ex_quantity; //可兑换库存
					});
					this.strtime = '2020-04-11 00:00:00';
					this.dateProceed();
				}
			},
			async getCartListLength() {
				let params = {},
					method = 'POST',
					data;
				// 用户未登录状态时
				if (!this.hasLogin) {
					let cartListLength = uni.getStorageSync('cartList').length;
					this.cartListLength = cartListLength > 0 ? cartListLength : 0;
					return
				}
				data = await Api.apiCall(method, Api.order.cartList, params);
				if (data) {
					this.cartListLength = data.cartList.reduce((prev, cur) => prev + cur.skuList.length, 0)
				}
			},
			mpNavbarClick() {
				let url = '/fortune/fortuneTask';
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url: url
				})
			},
			async changeFortuneTaskStatus() {
				let params = {},
					method = 'post',
					data = await Api.apiCall(method, Api.fortune.fortune_signin_list, params),
					status = (data && (data.logins[0].status == 0)) || !this.hasLogin;


				if (status) {
					// #ifdef APP-PLUS
					webView.showTitleNViewButtonRedDot({
						index: 0
					})
					// #endif
					// #ifdef H5

					document.querySelectorAll('.uni-page-head-ft .uni-page-head-btn')[0].classList.add('uni-page-head-btn-red-dot');
					// #endif
				} else {
					// #ifdef APP-PLUS
					webView.hideTitleNViewButtonRedDot({
						index: 0
					});
					// #endif
					// #ifdef H5
					if (document.querySelector('.uni-page-head-btn-red-dot')) {
						document.querySelector('.uni-page-head-btn-red-dot').classList.remove('uni-page-head-btn-red-dot');
					}
					// #endif
				}
				// webView.setTitleNViewButtonStyle(0, {
				// 	redDot: status
				// });
			},
			// 倒计时
			dateProceed() {
				setInterval(() => {
					let nowTime = (Date.parse(new Date())) / 1000;
					// var date = new Date(strtime); //传入一个时间格式，如果不传入就是获取现在的时间了，这样做不兼容火狐。
					// 可以这样做
					let date = new Date(this.strtime.replace(/-/g, '/'));
					let time3 = (Date.parse(date)) / 1000;
					let time4 = nowTime - time3;
					let timeDay = Math.floor(time4 / 60 / 60 / 24);
					let timeHour = Math.floor(time4 / 60 / 60) - timeDay * 24;
					if (timeHour < 10) {
						timeHour = '0' + timeHour
					}
					let timeMinute = Math.floor(time4 / 60) - timeDay * 24 * 60 - timeHour * 60;
					if (timeMinute < 10) {
						timeMinute = '0' + timeMinute
					}
					let timeSecond = Math.floor(time4) - timeDay * 24 * 60 * 60 - timeHour * 60 * 60 - timeMinute * 60;
					if (timeSecond < 10) {
						timeSecond = '0' + timeSecond
					}
					this.timeDay = timeDay; // 天
					this.timeHour = timeHour; // 小时
					this.timeMinute = timeMinute; // 分钟
					this.timeSecond = timeSecond; // 秒

				}, 1000);
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
			//详情页
			navToDetailPage(item) {
				let id = item.product_id;
				uni.navigateTo({
					url: `/mall/product/product?product_id=${id}`
				})
			},
			toPromotion(item) {
				let id = item.id,
					fortune = item.fortune;
				uni.navigateTo({
					url: `/mall/product/product?product_id=${id}&fortune=${fortune}`
				})
			},
			//本周新品
			toNewGoods() {
				uni.navigateTo({
					url: '/mall/new-goods/new-goods'
				})
			},
			//预售专区
			toPreSaleGoods() {
				uni.navigateTo({
					url: '/mall/pre-sale-goods/pre-sale-goods'
				})
			},
			toNoOpening() {
				uni.showToast({
					title: '暂未开通',
					icon: "none"
				});
			},
			async toMemberCentre() {
				if (this.hasLogin) {
					var params = {}
					let data = await Api.apiCall('post', Api.index.userInfo, params);
					let url;
					if (data) {
						if (data.type == 1 && data.agent_gif == 1) {
							url = '/pages/money/memberPaySuccess';
						} else if (data.type == 1 && data.agent_gif == 0) {
							url = '/member/myMembership';
						} else if (data.type == 2 && data.agent_gif == 1) {
							url = '/pages/money/memberPaySuccess';
						} else if (data.type == 2 && data.agent_gif == 0) {
							url = '/member/memberCenter';
						} else {
							url = '/member/memberActivation';
						}
						uni.navigateTo({
							url: url
						})
					}
				} else {
					uni.navigateTo({
						url: '/member/memberActivation'
					})
					return false;
				}
			}
		},
		// #ifndef MP
		// 标题栏input搜索框点击
		async onNavigationBarSearchInputClicked(e) {
			this.$api.msg('搜索功能暂未开放');
		},
		//点击导航栏 buttons 时触发
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				// #ifdef APP-PLUS
				// const pages = getCurrentPages();
				// const page = pages[pages.length - 1];
				// const currentWebview = page.$getAppWebview();
				// currentWebview.showTitleNViewButtonRedDot({ index:index })  
				// currentWebview.hideTitleNViewButtonRedDot({
				// 	index
				// });
				// #endif
				let url = '/fortune/fortuneTask';
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url: url
				})
			}
		}
		// #endif

	}
</script>

<style lang="scss">
	/* #ifdef MP */
	.mp-search-box {
		width: 600upx;
		height: 100%;
		display: flex;
		box-sizing: border-box;
		padding: 0 30upx;

		.ser-input {
			width: 100%;
			height: 56upx;
			line-height: 56upx;
			text-align: center;
			font-size: 28upx;
			color: $font-color-base;
			border-radius: 20px;
			background: rgba(230, 230, 230, 0.6);
		}
	}

	page {
		.cate-section {
			position: relative;
			z-index: 5;
			border-radius: 16upx 16upx 0 0;
			margin-top: -20upx;
		}

		.carousel-section {
			padding: 0;

			.titleNview-placing {
				padding-top: 0;
				height: 0;
			}

			.carousel {
				.carousel-item {
					padding: 0;
				}
			}

			.swiper-dots {
				left: 45upx;
				bottom: 40upx;
			}
		}
	}

	/* #endif */


	page {
		background: #f5f5f5;
	}

	.m-t {
		margin-top: 16upx;
	}

	/* 头部 轮播图 */
	.carousel-section {
		position: relative;

		// padding-top: 10px;
		margin-top: -90upx;

		.titleNview-placing {
			height: var(--status-bar-height);
			// padding-top: 44px;
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 540upx;
			transition: .4s;
		}
	}

	.carousel {
		width: 100%;
		height: 540upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			// padding: 0 28upx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}

	/* 分类 */
	.cate-section {
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-wrap: wrap;
		margin-top: 18upx;
		padding: 24upx 20upx;
		background: #fff;

		.cate-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
		}

		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 88upx;
			height: 90upx;
			margin-bottom: 14upx;
			border-radius: 50%;
			// opacity: .7;
			opacity: 1;
			box-shadow: 4upx 4upx 20upx rgba(250, 67, 106, 0.3);
		}
	}

	.ad-1 {
		width: 100%;
		height: 210upx;
		padding: 10upx 0;
		background: #fff;

		image {
			width: 100%;
			height: 100%;
		}
	}

	/* 猜你喜欢 */
	.Hobby {
		width: 100%;
		background: #fff;

		.s-header {
			display: flex;
			padding: 0 30upx;
			align-items: center;
			height: 88upx;
			line-height: 1;
			font-size: 36upx;
			color: #313332;
			font-weight: 600;

			.s-img {
				width: 32upx;
				height: 30upx;
				margin-right: 10upx;
			}

			text {
				color: #313332;
				font-weight: 300;
				font-size: $font-sm + 4upx;
			}

		}

		.product-list {
			width: 100%;
			padding: 0 4%;
			// margin-top: 20upx;
			display: flex;
			justify-content: space-between;

			.product {
				width: 48%;
				border-radius: 20upx;
				background-color: #fff;

				// box-shadow: 0upx 5upx 25upx rgba(0, 0, 0, 0.1);
				image {
					width: 326upx;
					height: 146upx;
				}
				.red{
					color: #ff0000;
				}

				.name {
					width: 100%;
					height: 36upx;
					margin: 10upx 0 15upx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					text-align: justify;
					overflow: hidden;
					font-size: 26upx;
					
				}

				.info {
					display: flex;
					justify-content: space-between;
					width: 100%;
					margin-bottom: 30upx;

					.price {
						color: #e65339;
						font-size: 30upx;
						font-weight: 600;

						.slogan {
							color: #807c87;
							margin-left: 20upx;
							font-size: 24upx;
							text-decoration: line-through;
						}
					}
				}
			}
		}

	}

	/* 秒杀专区 */
	.seckill-section {
		width: 100%;
		margin-top: 20upx;
		background: #fff;

		.s-header {
			display: flex;
			padding: 14upx 30upx;
			align-items: center;
			height: 92upx;
			line-height: 1;
			font-size: 36upx;
			color: #313332;
			font-weight: 600;

			.s-img {
				width: 26upx;
				height: 33upx;
				margin-right: 20upx;
				// border: 1upx solid red;
			}

			.tip {
				font-size: 36upx;
				color: #313332;
				margin: 0 20upx 0 0upx;
			}

			.timer {
				display: inline-block;
				width: 40upx;
				height: 36upx;
				text-align: center;
				line-height: 36upx;
				// margin-right: 14upx;
				// font-size: $font-sm+2upx;
				font-size: 24upx;
				color: #fff;
				border-radius: 2px;
				background: rgba(0, 0, 0, .8);
			}

			.colon {
				width: 20upx;
				text-align: center;
				color: #000000;
				position: relative;
				top: -4upx;
			}

			span {
				flex: 1;
				text-align: right;
				color: #A4A7A5;
				font-size: 24upx;

				.icon-you {
					// font-size: $font-lg;
					font-size: 24upx;
					color: $font-color-light;
				}
			}

		}

		.floor-list {
			white-space: nowrap;
		}

		.scoll-wrapper {
			display: flex;
			align-items: flex-start;
		}

		.floor-item {
			width: 100%;
			// margin-right: 20upx;
			font-size: $font-sm+2upx;
			color: $font-color-dark;
			// line-height: 1.8;

			image {
				width: 100%;
				height: 350upx;
			}

			.info {
				width: 100%;
				height: 158upx;
				padding: 20upx 4%;
				margin-top: -6upx;
				font-size: 30upx;

				.title {
					width: 100%;
					height: 60upx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.name {
						width: 50%;
						display: flex;
						justify-content: flex-start;
					}

					.snapUpNow {
						width: 27.5%;
						height: 100%;
						border-radius: 30upx;
						background-color: #FFBC21;
						display: flex;
						justify-content: center;
						align-items: center;
						color: #232323;
					}
				}

				.price-number {
					width: 100%;
					height: 58upx;
					display: flex;
					justify-content: space-between;
					align-items: flex-end;

					.price {
						color: #e65339;
						font-size: 30upx;
						font-weight: 600;

						.slogan {
							color: #807c87;
							margin-left: 20upx;
							font-size: 24upx;
							text-decoration: line-through;
						}
					}

					.sold {
						color: #807c87;
						font-size: 24upx;
						margin-right: 20upx;
						// text-decoration: line-through;
					}
				}
			}

		}
	}

	.bestSelling {
		width: 100%;
		margin-top: 20upx;
		margin-bottom: 10upx;
		background-color: #fff;

		.s-header {
			display: flex;
			padding: 0 30upx;
			align-items: center;
			height: 88upx;
			line-height: 1;
			font-size: 36upx;
			color: #313332;
			font-weight: 600;

			.s-img {
				width: 32upx;
				height: 28upx;
				margin-right: 10upx;
			}

			span {
				flex: 1;
				text-align: right;
				color: #A4A7A5;
				font-size: 24upx;

				.icon-you {
					// font-size: $font-lg;
					font-size: 24upx;
					color: $font-color-light;
				}
			}

		}

		.product-list {
			width: 100%;
			display: flex;
			flex-wrap: wrap;

			.goods-info {
				width: 100%;
				background-color: #fff;
				margin-bottom: 20upx;
				box-shadow: 0upx 5upx 25upx rgba(0, 0, 0, 0.1);

				.img {
					width: 100%;
					height: 350upx;
					display: flex;
					justify-content: center;

					image {
						width: 100%;
						height: 350upx;
					}
				}

				.info {
					width: 100%;
					height: 158upx;
					padding: 20upx 4%;
					font-size: 30upx;

					.title {
						width: 100%;
						height: 60upx;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.name {
							width: 50%;
							display: flex;
							justify-content: flex-start;
						}

						.snapUpNow {
							width: 27.5%;
							height: 100%;
							border-radius: 30upx;
							// background-color: #46B35C;
							background-color: #FFBC21;
							display: flex;
							justify-content: center;
							align-items: center;
							color: #232323;
						}
					}

					.price-number {
						width: 100%;
						height: 58upx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;

						.price {
							color: #e65339;
							font-size: 30upx;
							font-weight: 600;

							.slogan {
								color: #807c87;
								margin-left: 20upx;
								font-size: 24upx;
								text-decoration: line-through;
							}
						}

						.sold {
							color: #807c87;
							font-size: 24upx;
							margin-right: 20upx;
							text-decoration: line-through;
						}

						.hot {
							justify-content: center;
							color: #807c87;
							font-size: 24upx;
							margin-right: 20upx;

							image {
								width: 2.67vw;
								height: 2.30vw;
								margin-right: 10upx;
								align-items: flex-end;
							}
						}
					}
				}

			}
		}

	}

	.currency {
		font-size: 18upx;
	}
</style>
